<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="userList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hAlter(row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="queryParams.page"
                :page-sizes="[2, 3, 5, 10, 20]"
                :page-size="queryParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              /></el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 弹层 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" />
          </el-form-item>
        </el-form>

        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="200">
            <el-button
              size="small"
              @click="dialogVisible = false"
            >取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="hSubmit"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getUser, addUser, delUser, afterUser } from '@/api/setting'
export default {
  name: 'Setting',
  data() {
    return {
      isEdit: false, // false为新增弹层,true为编辑弹层,默认新增
      form: {
        description: '',
        name: ''
      },
      userList: [],
      queryParams: {
        page: 1, // 页码值 默认第一页
        pagesize: 2 // 每页显示的条数
      },
      total: 0,
      dialogVisible: false,
      rules: {
        description: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        name: [{ required: true, message: '请输入角色描述', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.doGet()
  },
  methods: {
    async doGet() {
      try {
        const res = await getUser(this.queryParams)
        this.userList = res.data.rows
        this.total = res.data.total
        console.log(res.data, '员工列表111')
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryParams.pagesize = val
      this.doGet()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryParams.page = val
      this.doGet()
    },

    // 提交表单
    hSubmit() {
      // 兜底校验
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        this.isEdit ? this.hMend() : this.hAppend()
      })
    },

    // 点击编辑按钮
    hAlter(data) {
      this.isEdit = true // 判断弹层为编辑业务
      this.dialogVisible = true // 显示弹层
      this.form = { ...data }
    },

    // 提交修改
    async hMend() {
      // 发送编辑请求
      await afterUser(this.form)
      // 重新加载数据
      this.doGet()
      // 关闭弹层
      this.dialogVisible = false
    },

    // 点击新增按钮
    hAdd() {
      this.dialogVisible = true
      this.isEdit = false // 控制弹层为新增业务
    },

    // 确定新增
    async  hAppend() {
      await addUser(this.form)
      // 添加成功后先增加总数, 在进行计算
      this.total++
      // 添加完毕后跳转到最后一页
      this.queryParams.page = Math.ceil(
        this.total / this.queryParams.pagesize
      )
      this.doGet()
      this.dialogVisible = false
    },

    // 删除数据
    hDel(id) {
      this.$confirm('确定删除吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 发送删除请求
        await delUser(id)
        if (this.userList.length === 1) {
          this.queryParams.page--
          if (this.queryParams.page <= 0) {
            this.queryParams.page = 1
          }
        }
        // 重新渲染数据
        this.doGet()
        this.$message.success('删除成功')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
